import {addGoodsToCart, getGoodsFromCart,updateCheckedForCartGood,delGoodsFromCart} from "http"
export default {
    state:{
        cartGoods:[]
    },
    getters:{
        //总数量
        totalNum(state) {
            /*let total = 0
            state.cartGoods.forEach((good) => {
                if (good.isChecked === 1)
                    total += good.skuNum
            })
            return total*/

            return state.cartGoods.reduce((adder,good)=>{
                return good.isChecked === 1 ? adder+=good.skuNum : adder
            },0)
        },
        //总价格
        totalPrice(state){
            /*let total = 0
            state.cartGoods.forEach((good)=>{
                if(good.isChecked === 1)
                    total+=good.skuNum * good.skuPrice
            })
            return total*/
            return state.cartGoods.reduce((adder,good)=>{
                return good.isChecked === 1 ? adder+=good.skuNum * good.skuPrice : adder
            },0)
        },
        //是否全选
        checkAll(state){
            return state.cartGoods.length>0 && state.cartGoods.every( good=>good.isChecked ===1)
        }
    },
    mutations:{
        getGoodsFromCart(state,cartGoods){
            state.cartGoods = cartGoods
        }
    },
    actions:{
        /*async addGoodsToCart1({},{skuid,skuNum}){
            const {code,message} = await addGoodsToCart(skuid,skuNum);
            return  {code,message}
        },*/
        async addGoodsToCart({},{skuid,skuNum}){
            try {
                //网络异常
                const {code,message} = await addGoodsToCart(skuid,skuNum);
                if(code!==200){
                    //不能直接抛异常  因为异常会被catch住
                    //请求成功了 但是添加购物车的操作没有成功
                    return message
                }
            }catch (e) {
                //网络问题进这个catch
                throw new Error("网络异常")
            }
        },
        //得到购物车数据
        async getGoodsFromCart({commit}){
            const {code,data} = await getGoodsFromCart();
            if(code === 200){
                commit("getGoodsFromCart",data)
            }
        },
        //去修改购物车中一条数据的选中状态
        async updateCheckedForCartGood({dispatch},{skuID,isChecked}){
            try {
                //调完整个updateCheckedForCartGood只是修改了数据库中的数据  仓库里面的并没有得到更新
                const {code,message} =await updateCheckedForCartGood(skuID,isChecked);
                if(code === 200){
                    //当数据库中的数据被成功修改后; 我们要重新去同步仓库中的cartGoods
                    //dispatch("getGoodsFromCart")
                }else {
                    return message
                }
            }catch (e){
                //网络问题进这个catch
                throw new Error("网络异常")
            }
        },
        //全选 全不选   只是去发请求改变数据库中的数据 而不是改变仓库中的数据
        async checkAllFn ({state,dispatch},checked){
            //存放所有promise的数组
            let allPromise = [];
            //checked:全选按钮当前最新的状态
            let isChecked = checked ?  1 : 0;
            //将购物车中所有商品的状态调整为checked对应的状态
            state.cartGoods.forEach((good)=>{
                if(good.isChecked !== isChecked){
                    //调整当前good的选中状态
                    const promise=dispatch("updateCheckedForCartGood",{skuID:good.skuId,isChecked})
                    allPromise.push(promise)
                }
            })
            return Promise.all(allPromise)
        },
        //删除购物车中一个商品
        async delGoodsFromCart({},skuId){
            const {code,message} = await delGoodsFromCart(skuId);
            if(code !== 200){
                //请求成功了 但删除操作出了异常
                throw new Error(message)
            }
        },
        //删除所有选中的商品
        async delAllCheckedFn({state,dispatch}){
            //存放所有promise的数组
            let allPromise = [];
            //state: 分仓库的state
            state.cartGoods.forEach(async (good)=>{
                if(good.isChecked === 1){
                    const promise =  dispatch("delGoodsFromCart",good.skuId)
                    allPromise.push(promise)
                }
            })
            return Promise.all(allPromise)
        },
    }
}
